import Vue from "vue";
import Vuex from "vuex";
import appConfig from "@/common/appConfig.js";

Vue.use(Vuex);


var store = new Vuex.Store({
	state: {
		appConfig: appConfig,
		session: {
			token: uni.getStorageSync("token"),
			schoolId: null,
			schoolName: null,
			studentNo: null,
			classId: null,
			className: null,
			userId: null,
			userName: null,
		},
		classLock: false, //教师上课锁 1完全锁定 2部分锁定 false无

		axiosPromiseCancelArr: [],
		version: {},

		uploadTaskList: [],

		// #ifdef APP-PLUS
		XFModule_FloatPermission: false,
		// #endif

		AppStatus: 0, //0 hide 1 show
	},
	getters: { //VueX的计算属性
		checkPermission: (state) => (permission) => {
			return !permission || state.session.permission_list.indexOf(permission) != -1
		},
		checkRole: (state) => (role) => {
			return state.session.role_list.indexOf(role) != -1
		},
	},
	mutations: {
		setSession(state, param) {
			state.session[param.attr] = param.target;
		},
		addAxiosPromiseCancel(state, newCancel) {
			state.axiosPromiseCancelArr.push(newCancel);
		},
		clearAxiosPromiseCancel(state) {
			state.axiosPromiseCancelArr = [];
		},
		logout(state) { //401时的登出或用户主动退出登录
			console.log("logout")
			state.session.token = null;
			state.session.schoolId = null;
			state.session.schoolName = null;
			state.session.studentNo = null;
			state.session.classId = null;
			state.session.className = null;
			state.session.userId = null;
			state.session.userName = null;

			uni.removeStorageSync('token');

			uni.reLaunch({
				url: "/pages/login/login"
			})
		},

		updateVersion(state, param) {
			state.version = param;
		},
		setAppStatus(state, param) {
			state.AppStatus = param;
			if (param) {
				console.log('App Show')
				// #ifdef APP-PLUS
				if (state.classLock == 2) { //部分锁定
					hideFloatView()
				}
				Vue.prototype.$type5Flag = false;//已退出图片预览/文件选择界面，恢复false
				// #endif
			} else {
				console.log("App Hide")
				// #ifdef APP-PLUS
				if (state.classLock == 2 && !Vue.prototype.$type5Flag) { //部分锁定
					showFloatView()
				}
				// #endif
			}
		},
		setClassLock(state, param) {
			state.classLock = param;

			// #ifdef APP-PLUS
			if (param == 1) {
				showFloatView();
			} else if (param == 2) {
				if (state.AppStatus == 0 && !Vue.prototype.$type5Flag) {
					showFloatView();
				} else if (state.AppStatus == 1) {
					hideFloatView()
				}
			} else if (!param) {
				hideFloatView()
			}
			// #endif
		},

		// #ifdef APP-PLUS
		setXFModule_FloatPermission(state, param) {
			state.XFModule_FloatPermission = param;
			//如果有悬浮窗权限则初始化悬浮窗
			if (state.XFModule_FloatPermission) {
				Vue.prototype.$XFModule.initFloatView((ret) => {
					console.log("================initFloatView================", ret.status)
					if (ret.status) {
						Vue.prototype.$XFModule.onClickListener(ret => {
							console.log("================onClickListener================")

							Vue.prototype.$XFModule.moveAppToFront(ret => {
								console.log(
									"================moveAppToFront================",
									ret.status
								)
							})

						})
					}
				})
			}
		}
		// #endif


	},

	actions: {

	},
});

store.setSession = function(attr, target) {
	store.commit("setSession", {
		attr,
		target
	});
};

function showFloatView() {
	//展示悬浮窗
	Vue.prototype.$XFModule.showFloatView((ret) => {
		console.log("================showFloatView================", ret)
		if (ret.status) {
			//使用悬浮窗文字。可以通过hideToast关闭文字，或通过hideFloatView直接关闭悬浮窗
			Vue.prototype.$XFModule.showToast({
				text: "上课中，请勿操作设备",
				textSize: 26
			}, function(ret) {
				console.log("================showToast================",
					ret.status)
			})
		}
	})
}

function hideFloatView() {
	//隐藏悬浮窗
	Vue.prototype.$XFModule.hideFloatView((ret) => {
		console.log("================hideFloatView================", ret)
		if (ret.status) {
			Vue.prototype.$XFModule.hideToast(function(ret) {
				console.log("================hideToast================",
					ret.status)
			})
		}
	})
}
export default store;